import sys
import os
import time
# import openpyxl
# from openpyxl.utils.cell import(
#     coordinate_to_tuple
# )
import xlwings as xw

def doSum(rows):
    # result = 0

    # for row in rows:
    #     result += row[0].value

    # return result
    return sum(rows.value)

def doAvg(rows):

    # result = 0

    # count = 0
    # for row in rows:
    #     result += row[0].value
    #     count += 1
    # result = result / count

    # return result
    
    sumVals = sum(rows.value)
    countVals = len(rows.value)

    return sumVals / countVals

#filePath = r"C:\test\v1\Excel.xlsm"
filePath = sys.argv[1]
#params = 'A1:A5000|SUM,B1:B5000|AVG'
params = sys.argv[2]
with open("temp.txt", "w") as f:
    f.write("\n".join(sys.argv))
# wb = openpyxl.load_workbook(filePath)
# sheet = wb['Sheet1']
wb = xw.Book(filePath)
sheet = wb.sheets['Sheet1']

# command example: 
#'A1:A5000|SUM' 
# from ['A1:A5000|SUM','B1:B5000|AVG']
for command in params.split(','):
	# commandParams example: 
	# 'A1:A5000|SUM' to ['A1:A5000','SUM']
    commandParams = command.split('|')

	# # coordinates example: 
	# # 'A1:A5000' to ['A1','A5000']
    # coordinates = commandParams[0].split(':')
    # # cstart example: 'A1' to (1,1)
    # cstart = coordinate_to_tuple(coordinates[0])
	# # cend example: 'A5000' to (5000,1)
    # cend = coordinate_to_tuple(coordinates[1])

    # # ### 2.1
    # rows = sheet.iter_rows(
    #     min_row = cstart[0], # 1
    #     max_row = cend[0], # 5000

    #     min_col = cstart[1], # 1
    #     max_col = cend[1] # 1
    # )

	# coordinates example: 'A1:A5000'
    coordinates = commandParams[0]
    rows = sheet.range(coordinates)

    # ### 2.2
    commandAction = commandParams[1]
    if commandAction == 'SUM':
        # ### 2.3 - print
        print(doSum(rows), flush=True)
        # input("sum")
    elif commandAction == 'AVG':
        print(doAvg(rows), flush=True)